package com.ruoyi.carbon.service.impl;

import com.ruoyi.carbon.domain.vo.BuhuoShenqingVo;
import com.ruoyi.carbon.entity.BuhuoShenqing;
import com.ruoyi.carbon.entity.BuhuoShenqingDetail;
import com.ruoyi.carbon.entity.BuhuoShenqingDetailWuliao;
import com.ruoyi.carbon.entity.Wuliao;
import com.ruoyi.carbon.mapper.BuhuoShenqingDetailMapper;
import com.ruoyi.carbon.mapper.BuhuoShenqingDetailWuliaoMapper;
import com.ruoyi.carbon.mapper.BuhuoShenqingMapper;
import com.ruoyi.carbon.mapper.WuliaoMapper;
import com.ruoyi.carbon.service.BuhuoShenqingService;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BuhuoShenqingServiceImpl implements BuhuoShenqingService {

    @Autowired
    private BuhuoShenqingMapper buhuoShenqingMapper;

    @Autowired
    private BuhuoShenqingDetailMapper buhuoShenqingDetailMapper;

    @Autowired
    private BuhuoShenqingDetailWuliaoMapper buhuoShenqingDetailWuliaoMapper;

    @Autowired
    private WuliaoMapper wuliaoMapper;


    @Override
    public List<BuhuoShenqing> list(BuhuoShenqing buhuoShenqing) {
        return buhuoShenqingMapper.list(buhuoShenqing);
    }

    @Override
    @Transactional
    public int remove(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) throw new ServiceException("请选择要删除的数据");

        List<Long> detailIds = buhuoShenqingDetailMapper.getIdByBhId(ids);

        if (CollectionUtils.isNotEmpty(detailIds)) {
            // 删除详细表和物料关联表
            buhuoShenqingDetailWuliaoMapper.removeByDetailIds(detailIds);

        }

        // 删除详细表
        buhuoShenqingDetailMapper.removeByBhIds(ids);

        // 删除补货申请
        return buhuoShenqingMapper.remove(ids);
    }

    @Override
    public BuhuoShenqingVo getById(Long id) {

        BuhuoShenqingVo buhuoShenqingVo = new BuhuoShenqingVo();

        BuhuoShenqing buhuoShenqing = buhuoShenqingMapper.getById(id);
        buhuoShenqingVo.setBuhuoShenqing(buhuoShenqing);

        List<BuhuoShenqingDetail> buhuoShenqingDetails = buhuoShenqingDetailMapper.getByBhId(id);

        if (ObjectUtils.isEmpty(buhuoShenqingDetails)) {
            return buhuoShenqingVo;
        }

        buhuoShenqingVo.setBuhuoShenqingDetails(buhuoShenqingDetails);

        // 获取详细表id
        List<Long> detailIds = buhuoShenqingDetails.stream().map(BuhuoShenqingDetail::getId).collect(Collectors.toList());

        // 查询物料id
        List<Long> wuliaoIds = buhuoShenqingDetailWuliaoMapper.getWuliaoId(detailIds);

        if (ObjectUtils.isEmpty(wuliaoIds)) {
            return buhuoShenqingVo;
        }

        // 查询物料数据
        List<Wuliao> wuliaos = wuliaoMapper.getByIds(wuliaoIds);

        buhuoShenqingVo.setWuliaos(wuliaos);

        return buhuoShenqingVo;
    }

    @Override
    @Transactional
    public int edit(BuhuoShenqingVo buhuoShenqingVo) {
        BuhuoShenqing buhuoShenqing = buhuoShenqingVo.getBuhuoShenqing();
        // 1. 修改补货申请
        int result = buhuoShenqingMapper.edit(buhuoShenqing);

        Long bhId = buhuoShenqing.getId();
        List<Long> detailIds = buhuoShenqingDetailMapper.getIdByBhId(Collections.singletonList(bhId));
        if (ObjectUtils.isNotEmpty(detailIds)) {

            // 2. 删除中间表
            buhuoShenqingDetailWuliaoMapper.removeByDetailIds(detailIds);
            // 3. 删除详情表
            buhuoShenqingDetailMapper.remove(detailIds);
        }


        // 4. 添加详情表
        List<BuhuoShenqingDetail> buhuoShenqingDetails = buhuoShenqingVo.getBuhuoShenqingDetails();
        // 4.1 判断是否有数据
        if (ObjectUtils.isEmpty(buhuoShenqingDetails)) {
            // 没有数据，直接返回
            return result;
        }
        // 有数据，添加
        buhuoShenqingDetails.forEach(item -> item.setBuhuoShenqingId(bhId));
        buhuoShenqingDetailMapper.batchSave(buhuoShenqingDetails);


        // 5. 添加关联表
        // 5.1 创建关联表集合
        List<BuhuoShenqingDetailWuliao> buhuoShenqingDetailWuliaos = new ArrayList<>();
        List<Wuliao> wuliaos = buhuoShenqingVo.getWuliaos();

        Long detailId = buhuoShenqingDetails.get(0).getId();
        wuliaos.forEach(
                item -> {
                    BuhuoShenqingDetailWuliao buhuoShenqingDetailWuliao = new BuhuoShenqingDetailWuliao();
                    buhuoShenqingDetailWuliao.setBuhuoShenqingDetailId(detailId);
                    buhuoShenqingDetailWuliao.setWuliaoId(item.getId());
                    buhuoShenqingDetailWuliaos.add(buhuoShenqingDetailWuliao);
                }
        );

        buhuoShenqingDetailWuliaoMapper.batchSave(buhuoShenqingDetailWuliaos);


        return result;
    }
}
